package cn.exrick.xboot.modules.base.serviceimpl.mybatis;

import cn.exrick.xboot.common.exception.ServiceException;
import cn.exrick.xboot.modules.base.dao.mapper.UserHierarchyMapper;
import cn.exrick.xboot.modules.base.entity.UserHierarchy;
import cn.exrick.xboot.modules.base.enums.OrganizationExceptionEnum;
import cn.exrick.xboot.modules.base.service.mybatis.IUserHierarchyService;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 兼职接口实现
 * @author njp
 */
@Slf4j
@Service
@Transactional
public class IUserHierarchyServiceImpl extends ServiceImpl<UserHierarchyMapper, UserHierarchy> implements IUserHierarchyService {
    @Autowired
    private UserHierarchyMapper userHierarchyMapper;
    @Override
    public void add(UserHierarchy userHierarchy) {
        if(ObjectUtil.isNull(userHierarchy.getType())){
            throw new ServiceException(OrganizationExceptionEnum.USERH_TYPE_IS_NOTNULL);
        }
        if(ObjectUtil.isNull(userHierarchy.getUserId())){
            throw new ServiceException(OrganizationExceptionEnum.USERH_USERID_IS_NOTNULL);
        }
        if(ObjectUtil.isNull(userHierarchy.getHierarchyId())){
            throw new ServiceException(OrganizationExceptionEnum.USERH_H_IS_NOTNULL);
        }
        this.save(userHierarchy);
    }

    @Override
    public void delete(UserHierarchy userHierarchy) {
        checkParam(userHierarchy,"delete");
        LambdaQueryWrapper<UserHierarchy> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(UserHierarchy::getUserId,userHierarchy.getUserId());
        lambdaQueryWrapper.eq(UserHierarchy::getHierarchyId,userHierarchy.getHierarchyId());
        this.remove(lambdaQueryWrapper);
    }

    @Override
    public List<UserHierarchy> getUserHierarchys(UserHierarchy userHierarchy) {
        checkParam(userHierarchy,"getUserHierarchys");
        return userHierarchyMapper.getUserHierarchys(userHierarchy);
    }

    /**
     * 参数校验
     */
    private void checkParam(UserHierarchy userHierarchy,String methodName){
        switch (methodName){
            case "delete":
                if(ObjectUtil.isNull(userHierarchy.getUserId())){
                    throw new ServiceException(OrganizationExceptionEnum.USERH_USERID_IS_NOTNULL);
                }
                if(ObjectUtil.isNull(userHierarchy.getHierarchyId())){
                    throw new ServiceException(OrganizationExceptionEnum.USERH_H_IS_NOTNULL);
                }
                if(ObjectUtil.isNull(userHierarchy.getType())){
                    throw new ServiceException(OrganizationExceptionEnum.USERH_TYPE_IS_NOTNULL);
                }
                break;
             case "getUserHierarchys":
                 if(ObjectUtil.isNull(userHierarchy.getUserId())){
                     throw new ServiceException(OrganizationExceptionEnum.USERH_USERID_IS_NOTNULL);
                 }
                 if(ObjectUtil.isNull(userHierarchy.getType())){
                     throw new ServiceException(OrganizationExceptionEnum.USERH_TYPE_IS_NOTNULL);
                 }
                 break;
            default:
                break;
        }
    }
}